//
//  TF_AlbumCell.swift
//  TalkfunSDKDemo
//
//  Created by 邓盛明 on 2021/12/7.
//  Copyright © 2021 Talkfun. All rights reserved.
//

import UIKit
import Lottie

class TF_AlbumCell: UITableViewCell {

    var imgView:UIImageView!
    var course:UILabel!
    var time:UILabel!
    var line:UIView!
    var onPlayBtn:UIButton!
    var playAnimation = TalkfunSharedTools.getJsonDataJsonname("TF_playAnimation")
    
    @objc public var cellwidth:CGFloat = 0
    @objc public var selectedArray:[Int] = []

    
    override init(style: UITableViewCell.CellStyle, reuseIdentifier: String?) {
        super.init(style: style, reuseIdentifier: reuseIdentifier)
        self.backgroundColor = UIColor.clear
        self.imgView = UIImageView.init()
        self.contentView.addSubview(self.imgView)
        
        self.course = UILabel.init()
        self.course.font = UIFont.PingFang(size: 14)
        self.course.textColor = UIColor(red: 0.08, green: 0.09, blue: 0.1,alpha:1)
        self.course.text = "第1页（00:23:01）"
        self.contentView.addSubview(self.course)
        
        self.time = UILabel.init()
        self.time.font = UIFont.PingFang(size: 14)
        self.time.textColor = UIColor(red: 0.39, green: 0.41, blue: 0.43,alpha:1)
        self.time.textAlignment = NSTextAlignment.left
        self.time.text = ""
        self.contentView.addSubview(self.time)
        
        self.onPlayBtn = UIButton.init()
        self.onPlayBtn.setTitle("播放中", for: .normal)
        self.onPlayBtn.setTitleColor(UIColor(red: 0.25, green: 0.59, blue: 0.92,alpha:1), for: .normal)
        self.onPlayBtn.titleLabel?.font = UIFont.PingFang(size: 14)
        self.onPlayBtn.isHidden = true
        self.contentView.addSubview(self.onPlayBtn)
        
       
        
        self.playAnimation.isHidden = true
        self.contentView.addSubview(self.playAnimation)

        self.line = UIView.init()
        self.line.backgroundColor = UIColor(red: 0.86, green: 0.89, blue: 0.92, alpha: 1)
        self.contentView.addSubview(self.line)
        
    }
    
    override func layoutSubviews() {
        super.layoutSubviews()
        self.imgView.frame = CGRect(x: 16, y: 12, width: 98, height: 66)
        self.course.frame = CGRect(x: self.imgView.frame.maxX+8, y: 12, width: self.cellwidth-self.imgView.frame.maxX-8, height: 46)
        self.time.frame = CGRect(x: self.imgView.frame.maxX+8, y: self.frame.size.height-12-20, width: self.cellwidth-self.imgView.frame.maxX-8, height: 20)
        
     
        self.onPlayBtn.frame = CGRect(x: self.cellwidth-45-24, y: (self.frame.size.height-20)/2.0, width: 45, height: 20)
        self.playAnimation.frame = CGRect(x: self.onPlayBtn.frame.origin.x-25, y: (self.frame.size.height-20)/2.0, width: 20, height: 20)
        self.line.frame = CGRect(x: 0, y: self.frame.size.height-1, width: self.cellwidth, height: 1)
    }
    
    
    @objc public func setAlbumModel(model:TFPlaybackAlbum) {
        
    
  
      
        
    
        self.course.text = model.title;
      
        self.time.text = self.transToHourMinSec(time: Float(model.duration))
      

        let url:URL? = URL.init(string: model.img_small)
        self.imgView.sd_setImage(with: url, completed: nil)
 
        if (model.select) {
            self.onPlayBtn.isHidden = false
           
            self.playAnimation.isHidden = false
            self.backgroundColor  = UIColor(red: 0.79, green: 0.83, blue: 0.87, alpha: 0.2)
           
        }
        else
        {
            self.onPlayBtn.isHidden = true
           
            self.playAnimation.isHidden = true
            self.backgroundColor = UIColor.clear
           
        }
        
        

        
    }
    func transToHourMinSec(time: Float) -> String
    {
        let allTime: Int = Int(time)
        var hours = 0
        var minutes = 0
        var seconds = 0
        var hoursText = ""
        var minutesText = ""
        var secondsText = ""
        
        hours = allTime / 3600
        hoursText = hours > 9 ? "\(hours)" : "0\(hours)"
        
        minutes = allTime % 3600 / 60
        minutesText = minutes > 9 ? "\(minutes)" : "0\(minutes)"
        
        seconds = allTime % 3600 % 60
        secondsText = seconds > 9 ? "\(seconds)" : "0\(seconds)"
        
        return "\(hoursText):\(minutesText):\(secondsText)"
    }
   
    
    required init?(coder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }


    override func setSelected(_ selected: Bool, animated: Bool) {
        super.setSelected(selected, animated: animated)

        // Configure the view for the selected state
    }

}
